#Written by MMCOWBOY
import pygame
import math
import weapon, effect

TYPE_NONE = 0
TYPE_BOMB = 1
TYPE_HP = 2
TYPE_SHIELD = 3
TYPE_MISSILE_LUNCHER = 4
TYPE_RED_LASER_GUN = 5
UP_PARTITION = 3.0 / 7.0

def build_item(item_type, screen_size, position):
    if item_type == TYPE_NONE :
        return
    if item_type == TYPE_BOMB :
        bomb_item(screen_size, position)
    elif item_type == TYPE_HP :
        hp_item(screen_size, position)
    elif item_type == TYPE_SHIELD :
        shield_item(screen_size, position)
    elif item_type == TYPE_MISSILE_LUNCHER :
        missile_luncher_item(screen_size, position)
    elif item_type == TYPE_RED_LASER_GUN :
        red_laser_gun_item(screen_size, position)
    else:
        pass
        

class item_obj(pygame.sprite.Sprite):
    
    TYPE_BOMB = 1
    TYPE_HP = 2
    TYPE_SHIELD = 3
    TYPE_MISSILE_LUNCHER = 4
    TYPE_RED_LASER_GUN = 5
    UP_PARTITION = 3.0 / 7.0
    
    def __init__(self, screen_size, position):
        super(item_obj,self).__init__(self.containers)
        self.screen_size = screen_size                          #screen size
        self.screen_width = screen_size[0]                      #screen's width
        self.screen_height = screen_size[1]                     #screen's height
        self.speed = 3                                          #propertiy's speed
        self.step_counter = 0
        self.image = None
        self.rect = None
        self.add_point = 0

    def move(self):
        MAX_MOVE_RANGE = 5
        speed_x = MAX_MOVE_RANGE * math.sin(self.step_counter)
        speed_y = MAX_MOVE_RANGE * math.cos(self.step_counter)
        if (speed_y < 0):
            speed_y = speed_y * self.UP_PARTITION
        if (speed_y > 0 and self.rect.top >= self.screen_height +  self.rect.height / 2.0 + MAX_MOVE_RANGE):
            self.kill()
        self.rect = self.rect.move(speed_x, speed_y)
        self.step_counter -= 0.125
                    
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
        
    def update(self):
        self.move()
        
class hp_item(item_obj):
    
    def __init__(self, screen_size, position):
        super(hp_item, self).__init__(screen_size, position)
        self.image = pygame.image.load("data/images/p_hp.png")
        self.add_point = 0.5
        self.rect = self.image.get_rect(center = position)
        self.rect = self.rect.move(0, -self.rect.height / 2.0)

    def apply(self, object):
        object.hp += object.max_hp * self.add_point
        if object.hp > object.max_hp:
            object.hp = object.max_hp
        apply_sound = pygame.mixer.Sound("data/sounds/hp_apply.wav")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        self.kill()
        
        
class bomb_item(item_obj):

    def __init__(self, screen_size, position):
        super(bomb_item, self).__init__(screen_size, position)
        self.image = pygame.image.load("data/images/p_bomb.png")   
        self.add_point = 1                         
        self.rect = self.image.get_rect(center = position)
        self.rect = self.rect.move(0, -self.rect.height / 2.0)
        
    def apply(self, object):
        object.bomb_list.append(self.add_point)
        apply_sound = pygame.mixer.Sound("data/sounds/bomb_apply.wav")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        self.kill()
        
class shield_item(item_obj):
    
    def __init__(self, screen_size, position):
        super(shield_item, self).__init__(screen_size, position)
        self.image = pygame.image.load("data/images/p_shield.png")   
        self.add_point = 500                         
        self.rect = self.image.get_rect(center = position)
        self.rect = self.rect.move(0, -self.rect.height / 2.0)
        
    def apply(self, object):
        if object.super_shield:
            object.super_shield.kill()
        object.super_shield = shield_obj(object, self.add_point)
        apply_sound = pygame.mixer.Sound("data/sounds/shield_apply.WAV")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        self.kill()

class missile_luncher_item(item_obj):
    
    def __init__(self, screen_size, position):
        super(missile_luncher_item, self).__init__(screen_size, position)
        self.image = pygame.image.load("data/images/p_missile.png")   
        self.add_point = 100                      
        self.rect = self.image.get_rect(center = position)
        self.rect = self.rect.move(0, -self.rect.height / 2.0)
        
    def apply(self, object):
        if object.additional_weapon:
            object.additional_weapon.kill()
        object.additional_weapon = missile_luncher_obj(object, self.add_point)
        apply_sound = pygame.mixer.Sound("data/sounds/w_apply.WAV")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        self.kill()
        
class red_laser_gun_item(item_obj):
    
    def __init__(self, screen_size, position):
        super(red_laser_gun_item, self).__init__(screen_size, position)
        self.image = pygame.image.load("data/images/p_redlaser.png")   
        self.add_point = 1000                         
        self.rect = self.image.get_rect(center = position)
        self.rect = self.rect.move(0, -self.rect.height / 2.0)
        
    def apply(self, object):
        if object.additional_weapon:
            object.additional_weapon.kill()
        object.additional_weapon = red_laser_gun(object, self.add_point)
        apply_sound = pygame.mixer.Sound("data/sounds/w_apply.WAV")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        self.kill()
        
class shield_obj(pygame.sprite.Sprite):
    
    def __init__(self, belong_to, last_time):
        super(shield_obj, self).__init__(self.containers)
        self.image = pygame.image.load("data/images/shield.png")
        self.rect = self.image.get_rect(center = belong_to.rect.center)
        self.last_time = last_time
        self.belong_to = belong_to
        self.hp = 1000                      # the hp of the shield
        self.power = 10                    # harm point of the shield
        
    def destroied(self, opponent):
        self.hp -= opponent.power
        if (self.hp <= 0):
            self.crashed()
            
    def crashed(self):
        apply_sound = pygame.mixer.Sound("data/sounds/shield_unmount.wav")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        
        self.belong_to.super_shield = None
        self.belong_to.set_harm_less()
        self.kill()
        
        
    def update(self):
        self.last_time -= 1
#        #print "last_time: ",self.last_time
        self.rect.center = self.belong_to.rect.center
        if (self.last_time == 0):
            self.crashed()
            
    def draw(self, screen):
        screen.blit(self.image, self.rect)

class red_laser_gun(pygame.sprite.Sprite):
    FIRE_INTV = 4
    
    def __init__(self, belong_to, shot_num):
        super(red_laser_gun, self).__init__(self.containers)
        self.belong_to = belong_to
        self.shot_num = shot_num
        self.fire_intv = self.FIRE_INTV
        self.image = pygame.image.load("data/images/red_laser_gun.PNG")
        self.rect = self.image.get_rect(center = self.belong_to.rect.center)
        
        self.shot_sound = pygame.mixer.Sound("data/sounds/w_1_player.wav")
        self.shot_sound.set_volume(0.5)
        
        apply_sound = pygame.mixer.Sound("data/sounds/w_apply.wav")
        apply_sound.set_volume(1.0)
        apply_sound.play()
        
        
    def fire(self):
        if (self.shot_num <= 0):
            unmount_sound = pygame.mixer.Sound("data/sounds/w_unmount.wav")
            unmount_sound.set_volume(1.0)
            unmount_sound.play()
            
            self.kill()
            
            return
            
        if (self.fire_intv == 0):
            self.fire_intv = self.FIRE_INTV
            self.shot_sound.play()
            weapon.shot_obj(self.belong_to.screen_size, 10, -10, (self.rect.centerx + 10,self.rect.centery),\
                                    self.belong_to, weapon.shot_obj.LASER_RED, -45)
            weapon.shot_obj(self.belong_to.screen_size, -10, -10, (self.rect.centerx - 10,self.rect.centery),\
                                    self.belong_to, weapon.shot_obj.LASER_RED, 45)
            weapon.shot_obj(self.belong_to.screen_size, 7.5, -13, (self.rect.centerx + 10 ,self.rect.centery),\
                                    self.belong_to, weapon.shot_obj.LASER_RED, -30)
            weapon.shot_obj(self.belong_to.screen_size, -7.5, -13, (self.rect.centerx - 10,self.rect.centery),\
                                    self.belong_to, weapon.shot_obj.LASER_RED, 30)
            self.shot_num -= 4
        else:
            self.fire_intv -= 1
        
    def update(self):
        self.rect.center = self.belong_to.rect.center
        self.fire()
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
    

class missile_luncher_obj(pygame.sprite.Sprite):
    FIRE_INTV = 20
    
    def __init__(self, belong_to, missile_num):
        super(missile_luncher_obj, self).__init__(self.containers)
        self.belong_to = belong_to
        self.missile_num = missile_num
        self.fire_intv = self.FIRE_INTV
        self.image = pygame.image.load("data/images/missile_luncher.PNG")
        self.rect = self.image.get_rect(center = self.belong_to.rect.center)
        self.shoot_sound = pygame.mixer.Sound("data/sounds/missile_lunch.WAV")
        self.shoot_sound.set_volume(0.5)
        
    def fire(self):
        if (self.missile_num <= 0):
            
            unmount_sound = pygame.mixer.Sound("data/sounds/w_unmount.wav")
            unmount_sound.set_volume(1.0)
            unmount_sound.play()
            
            self.kill()
            return
            
        if (self.fire_intv == 0):
            self.fire_intv = self.FIRE_INTV
            #print "fire missile!"
            self.shoot_sound.play()
            if self.missile_num > 0 :
                weapon.missile_obj(self.belong_to.screen_size, (self.rect.centerx - 10, self.rect.centery)\
                , self.belong_to)
                self.missile_num -= 1
            if self.missile_num > 0 :
                weapon.missile_obj(self.belong_to.screen_size, (self.rect.centerx + 10, self.rect.centery)\
                , self.belong_to)
                self.missile_num -= 1
        else:
            self.fire_intv -= 1
        
    def update(self):
        self.rect.center = self.belong_to.rect.center
        self.fire()
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
        
class item_craft_obj(pygame.sprite.Sprite):
    
    SPARK_INTV = 2
    DEFAULT_HP = 20
    YELLOW_CRAFT = 1
    RED_CRAFT = 2
    
    def __init__(self, screen_size, position, sub_type, item_type):
        super(item_craft_obj, self).__init__(self.containers)
        self.screen_width = screen_size[0]
        self.screen_height = screen_size[1]
        self.screen_size = screen_size
        self.sub_type = sub_type
        self.flame_spark_intv = self.SPARK_INTV
        self.speed = 4

        #----
        self.is_item = True
        
        if (sub_type == self.YELLOW_CRAFT):
            self.norm_image = pygame.image.load("data/images/i_c1.png")
            self.accl_image = pygame.image.load("data/images/i_c1accel.png")
            self.image = self.norm_image
            self.rect = self.image.get_rect(center = position)
            self.hp = self.DEFAULT_HP
            self.explos_sound = pygame.mixer.Sound("data/sounds/explos_enemy.WAV")
            self.explos_sound.set_volume(1.0)

        else:
            pass
        
        self.item_type = item_type
        
    def destroied(self, opponent): #called by the shot that hit the obj
    	
        #print 'destroied in ic'
    	
        if opponent.power > 0:
            self.hp -= opponent.power
            if (self.hp <= 0):
                self.crashed()
        else:
            self.crashed()
            
    def crashed(self):
        self.explos_sound.play()
        self.kill()
        effect.spark_obj(self.rect.center)
        #flame.explosion(self.rect.center)
        #flame...
        build_item(self.item_type, self.screen_size, self.rect.center)
        
                    
        
    def move(self):
        if (self.rect.top + self.speed >= self.screen_height):
            self.kill()
        else:
            self.rect = self.rect.move(0, self.speed)
    
    def update(self):
        if (self.flame_spark_intv == 0):
            self.flame_spark_intv = self.SPARK_INTV
            self.image = self.accl_image
        else:
            self.flame_spark_intv -= 1
            self.image = self.norm_image
            
        self.move()
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
    def is_alived(self):
        return self.hp > 0
    
